package com.eascs.p2p.admin.service.impl;

import java.util.List;

import javax.inject.Inject;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.admin.manager.IAdminRoleManager;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.entity.AdminRoleResourceEntity;
import com.eascs.p2p.common.service.IAdminRoleService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.common.vo.UserRoleVo;

@Service
public class AdminRoleServiceImpl implements IAdminRoleService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Inject
	IAdminRoleManager adminRoleManager;


	@Override
	public ModelResult<List<Object[]>> getRoleName(String keywords) {
		
		ModelResult<List<Object[]>> result = new ModelResult<List<Object[]>>();
		try {
			result.setModel(adminRoleManager.getRoleName(keywords));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("用户角色联想登录：", e.getMessage());
		}
		return result;
	}
	
	@Override
	public ModelResult<PageResult<Object[]>> selectRolePage(UserRoleVo role) {
		ModelResult<PageResult<Object[]>> result = new ModelResult<PageResult<Object[]>>();
		try {
			result.setModel(adminRoleManager.selectRolesPage(role));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("用户角色分页查询：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<String> insertRole(AdminRoleEntity role) {
		ModelResult<String> result = new ModelResult<String>();
		try {
			result.setModel(adminRoleManager.insertRole(role));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("用户角色添加：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<Boolean> updateRole(AdminRoleEntity role) {
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			result.setModel(adminRoleManager.updateRole(role));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("更新用户角色：", e.getMessage());
		}
		return result;
	}

	


	@Override
	public ModelResult<AdminRoleEntity> getAdminRoleById(String roleId) {
	
		ModelResult<AdminRoleEntity> result = new ModelResult<AdminRoleEntity>();
		try {
			result.setModel(adminRoleManager.getAdminRoleById(roleId));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("根据角色id查询角色：", e.getMessage());
		}
		return result;
	}

	
	@Override
	public ModelResult<Boolean> deleteRole(String roleId) {
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			boolean flag  = false;
			boolean flag1 = true;
			boolean flag2 = true;
			if(adminRoleManager.isExistRoleUser(roleId)){
				 flag1 = adminRoleManager.deleteRoleUser(roleId);
			}
			if(adminRoleManager.isExistRoleRes(roleId)){
				flag2 = adminRoleManager.deleteRoleRes(roleId);
			}
			boolean flag3 = adminRoleManager.deleteRole(roleId);
			if(flag1&&flag2&&flag3){
				flag=true;
			}
			result.setModel(flag);
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("删除角色：", e.getMessage());
		}
		return result;
	}

	@Override
	public ModelResult<Boolean> deleteRoleUser(String roleId) {
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			result.setModel(adminRoleManager.deleteRoleUser(roleId));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("删除角色资源：", e.getMessage());
		}
		return result;
	}
	
	@Override
	public ModelResult<Boolean> deleteRoleRes(String roleId) {
		
		ModelResult<Boolean> result = new ModelResult<Boolean>();
		try {
			result.setModel(adminRoleManager.deleteRoleRes(roleId));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("删除角色资源：", e.getMessage());
		}
		return result;
	}
	
	
	@Override
	public ModelResult<Boolean> insertRoleResBatch(List<AdminRoleResourceEntity> roleResList) {
		ModelResult<Boolean> modelResult=new ModelResult<Boolean>();
		try {
			String roleId=roleResList.get(0).getRoleId();
			adminRoleManager.deleteRoleRes(roleId);
			boolean result = adminRoleManager.insertRoleResBatch(roleResList);
			modelResult.setModel(result);
    		return modelResult;
		} catch (Exception e) {
			 logger.error("批量：新增角色资源失败:", e);
			 modelResult.withError("SysPermissionServiceImpl.insertRoleResBatch", "批量：新增角色资源失败:"+e.getMessage());
			 return modelResult;
		}
	}
	
	@Override
	public ModelResult<AdminRoleEntity> insertRoleReturnRole(AdminRoleEntity role) {
		ModelResult<AdminRoleEntity> modelResult=new ModelResult<>();
		insertRole(role);
		modelResult.setModel(role);
		return modelResult;
	}
	
	@Override
	public ModelResult<List<AdminRoleEntity>> selectRolesByUserNo(String userNo) {
	
		ModelResult<List<AdminRoleEntity>> result = new ModelResult<List<AdminRoleEntity>>();
		try {
			result.setModel(adminRoleManager.selectRolesByUserNo(userNo));
		} catch (Exception e) {
			e.printStackTrace();
			result.withError("根据用户名称查询用户角色：", e.getMessage());
		}
		return result;
	}

	
}
